﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RIS.TMP.WEB.APP.Models;
using RIS.TMP.WEB.APP.AddMigrations;
using RIS.TMP.WEB.APP.App_Start;
using System.Web.Routing;

namespace RIS.TMP.WEB.APP.Controllers
{
    public class MRestoreToRepairController : Controller
    {
        private AllContext db = new AllContext();

        //
        // GET: /MRestoreToRepair/

        public ActionResult Index()
        {
            var mrestoretorepairs = db.MRestoreToRepairs.Include(m => m.MRestoreToAdmin).Include(m => m.CCheckPlan).Include(m => m.COutsource).Include(m => m.CStatusRepair).Where(x => x.RecordStatus == "U");
            return View(mrestoretorepairs.ToList());
        }

        //
        // GET: /MRestoreToRepair/Details/5

        public ActionResult Details(int id = 0)
        {
            MRestoreToRepair mrestoretorepair = db.MRestoreToRepairs.Find(id);
            if (mrestoretorepair == null)
            {
                return HttpNotFound();
            }
            return View(mrestoretorepair);
        }

        //
        // GET: /MRestoreToRepair/Create

        public ActionResult Create(int Id)
        {
           
            ViewBag.ResId = new SelectList(db.MRestoreToAdmins, "ResId", "ResCode");
            ViewBag.ChpId = new SelectList(db.CCheckPlans, "ChpId", "ChpName");
            ViewBag.OutId = new SelectList(db.COutsources, "OutId", "OutName");
            ViewBag.StatusReId = new SelectList(db.CStatusRepairs, "StatusReId", "StatusReName");
            return View();
        }

        //
        // POST: /MRestoreToRepair/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MRestoreToRepair mrestoretorepair, int id)
        {
            if (ModelState.IsValid)
            {
                db.MRestoreToRepairs.Add(mrestoretorepair);
                mrestoretorepair.RecordStatus = "U";
                mrestoretorepair.CreateBy = Session["LogedId"].ToString();
                mrestoretorepair.CreateWhen = DateTime.Now;
                mrestoretorepair.ResId = id;
                var product = (from p in db.MRestoreToAdmins
                               where p.ResId == mrestoretorepair.ResId
                               select p).Single();

                product.MRestore = 2;

                db.SaveChanges();

                return RedirectToAction("Index", "MRestoreToRepair");

                //return RedirectToAction("Index");
            }

            ViewBag.ResId = new SelectList(db.MRestoreToAdmins, "ResId", "ResCode", mrestoretorepair.ResId);
            ViewBag.ChpId = new SelectList(db.CCheckPlans, "ChpId", "ChpName", mrestoretorepair.ChpId);
            ViewBag.OutId = new SelectList(db.COutsources, "OutId", "OutName", mrestoretorepair.OutId);
            ViewBag.StatusReId = new SelectList(db.CStatusRepairs, "StatusReId", "StatusReName", mrestoretorepair.StatusReId);
            return View(mrestoretorepair);
        }

        //
        // GET: /MRestoreToRepair/Edit/5

        public ActionResult Edit(int id = 0)
        {
            MRestoreToRepair mrestoretorepair = db.MRestoreToRepairs.Find(id);
            if (mrestoretorepair == null)
            {
                return HttpNotFound();
            }
            ViewBag.ResId = new SelectList(db.MRestoreToAdmins, "ResId", "ResCode", mrestoretorepair.ResId);
            ViewBag.ChpId = new SelectList(db.CCheckPlans, "ChpId", "ChpName", mrestoretorepair.ChpId);
            ViewBag.OutId = new SelectList(db.COutsources, "OutId", "OutName", mrestoretorepair.OutId);
            ViewBag.StatusReId = new SelectList(db.CStatusRepairs, "StatusReId", "StatusReName", mrestoretorepair.StatusReId);
            return View(mrestoretorepair);
        }

        //
        // POST: /MRestoreToRepair/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(MRestoreToRepair mrestoretorepair)
        {
            if (ModelState.IsValid)
            {
                db.Entry(mrestoretorepair).State = EntityState.Modified;
                mrestoretorepair.UpdateBy = Session["LogedId"].ToString();
                mrestoretorepair.UpdateWhen = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ResId = new SelectList(db.MRestoreToAdmins, "ResId", "ResCode", mrestoretorepair.ResId);
            ViewBag.ChpId = new SelectList(db.CCheckPlans, "ChpId", "ChpName", mrestoretorepair.ChpId);
            ViewBag.OutId = new SelectList(db.COutsources, "OutId", "OutName", mrestoretorepair.OutId);
            ViewBag.StatusReId = new SelectList(db.CStatusRepairs, "StatusReId", "StatusReName", mrestoretorepair.StatusReId);
            return View(mrestoretorepair);
        }

        //
        // GET: /MRestoreToRepair/Delete/5

        public ActionResult Delete(int id = 0)
        {
            MRestoreToRepair mrestoretorepair = db.MRestoreToRepairs.Find(id);
            if (mrestoretorepair == null)
            {
                return HttpNotFound();
            }
            return View(mrestoretorepair);
        }

        //
        // POST: /MRestoreToRepair/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            MRestoreToRepair mrestoretorepair = db.MRestoreToRepairs.Find(id);
            //db.MRestoreToRepairs.Remove(mrestoretorepair);
            mrestoretorepair.RecordStatus = "D";
            mrestoretorepair.UpdateBy = Session["LogedId"].ToString();
            mrestoretorepair.UpdateWhen = DateTime.Now;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}